package csel.model.areaeffect;

import csel.model.Ticked;
import csel.model.GameGlobals;
import csel.model.entity.Entity;

public abstract class TimedEffect implements Ticked, AreaEffect{
	private int rate;
	private GameGlobals gameGlobals;
	private int ticksToEffect;
	private Entity myEntity;
	
	public TimedEffect(int rate)
	{
		this.rate = rate;
	}
	
	public int getRate()
	{
		return(this.rate);
	}
	
	public void leave(Entity e)
	{
		myEntity = null;
		gameGlobals.getChronoTrigger().removeTicked(this);
	}
	
	public void attach(GameGlobals g)
	{
		this.gameGlobals = g;
	}
	
	public void enter(Entity e)
	{
		ticksToEffect = -1;
		myEntity = e;
		gameGlobals.getChronoTrigger().addTicked(this);
	}
	
	protected abstract void doEffect(Entity e);
	
	public void tick()
	{
		if(ticksToEffect >= 0)
		{
			ticksToEffect--;
		}
		else
		{
			this.doEffect(myEntity);
			ticksToEffect = 20;
		}
	}
}
